/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 *
 * @author kashif
 */
public class MailUtility {
    
    private static final String SMTP_CONFIG_FILE_NAME="smtp_config.properties";
    private static Properties SMTP_CONFIG=null;

    public static boolean sendMail(String emailTo, String emailFrom, String mailCotent, String subject) {
        boolean flage = true;
        String[] to = {emailTo};
       // String subject = "Your MedBill Password";
        try {
            //postMail(to, subject, mailCotent, emailFrom);
            doMail(to, subject, mailCotent, emailFrom);
        } catch (MessagingException e) {
            e.printStackTrace();
            flage = false;
        }


        return flage;
    } // end of send mail method

    /*private static void postMail(String recipients[], String subject,
            String message, String from) throws MessagingException {
        boolean debug = false;
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// Set the host smtp address
        Properties properties = new Properties();
        properties.put("mail.smtp.user", "saifytarar@gmail.com");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtps.auth", "true ");
        properties.put("mail.smtp.debug", "true");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.socketFactory.fallback", "false");

// create some properties and get the default Session

        Session session = Session.getDefaultInstance(properties, null);

        session.setDebug(true);
        session.setDebug(debug);

// create a message
        MimeMessage msg = new MimeMessage(session);

// set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }

        msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport tr = session.getTransport("smtp");

// Here dummy userid and password are used, replace them with your own
// gmail userid and password
        tr.connect("smtp.gmail.com", "saifytarar@gmail.com", "");
        msg.saveChanges();
        tr.sendMessage(msg, msg.getAllRecipients());
        tr.close();

    //System.out.println("Message sent");
    }*/
    
    private static void doMail(String recipients[], String subject,
            String message, String from) throws MessagingException {
        boolean debug = true;
                       
        Properties props = loadSMTPConfig();
        
        Session session = Session.getDefaultInstance(props,null);
        session.setDebugOut(System.out);
        session.setDebug(debug);

        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(props.getProperty("mail.smtp.from"));
        msg.setFrom(addressFrom);
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }
    
    private static Properties loadSMTPConfig(){
        try {
            SMTP_CONFIG = new Properties();
            SMTP_CONFIG.load(MailUtility.class.getResourceAsStream(SMTP_CONFIG_FILE_NAME));
        } catch (IOException ex) {
            Logger.getLogger(MailUtility.class.getName()).log(Level.SEVERE, null, ex);
        }
        //message = SMTP_CONFIG.getProperty("message");
        //System.out.println(message);
        return SMTP_CONFIG;
    }
    
    public static void main(String arg[]){
        String emailContents = "Dear Rizwan, \nPlease find the your password here for pms user\n\\n User Id: UserID \n Password: Password \n\n\nGood Luck\n\nPMS Password Recovery Service.";
        boolean mailStatus = MailUtility.sendMail("rizwan.yq@gmail.com", "admin@medbill.com", emailContents, "Your MedBill Password");
    }
}
